<?php
if ( function_exists('register_sidebar') )
    register_sidebar(array(
        'before_widget' => '<div id="%1$s" class="widget %2$s"><div class="widgetTop"></div>',
        'after_widget' => '</div>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ));

/* 
Plugin Name: WP-PageNavi 
Plugin URI: http://www.lesterchan.net/portfolio/programming.php 
*/ 

function wp_pagenavi($before = '', $after = '', $prelabel = '', $nxtlabel = '', $pages_to_show = 5, $always_show = false) {
	global $request, $posts_per_page, $wpdb, $paged;
	if(empty($prelabel)) {
		$prelabel  = '<strong>&laquo;</strong>';
	}
	if(empty($nxtlabel)) {
		$nxtlabel = '<strong>&raquo;</strong>';
	}
	$half_pages_to_show = round($pages_to_show/2);
	if (!is_single()) {
		if(!is_category()) {
			preg_match('#FROM\s(.*)\sORDER BY#siU', $request, $matches);		
		} else {
			preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches);		
		}
		$fromwhere = $matches[1];
		$numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere");
		$max_page = ceil($numposts /$posts_per_page);
		if(empty($paged)) {
			$paged = 1;
		}
		if($max_page > 1 || $always_show) {
			echo "$before <div class='Nav'><span>Pages ($max_page): </span>";
			if ($paged >= ($pages_to_show-1)) {
				echo '<a href="'.get_pagenum_link().'">&laquo; First</a> ... ';
			}
			previous_posts_link($prelabel);
			for($i = $paged - $half_pages_to_show; $i  <= $paged + $half_pages_to_show; $i++) {
				if ($i >= 1 && $i <= $max_page) {
					if($i == $paged) {
						echo "<strong class='on'>$i</strong>";
					} else {
						echo ' <a href="'.get_pagenum_link($i).'">'.$i.'</a> ';
					}
				}
			}
			next_posts_link($nxtlabel, $max_page);
			if (($paged+$half_pages_to_show) < ($max_page)) {
				echo ' ... <a href="'.get_pagenum_link($max_page).'">Last &raquo;</a>';
			}
			echo "</div> $after";
		}
	}
}


/*
Plugin Name: Official Comments
Version: 1.1
Plugin URI: http://inner.geek.nz/archives/2005/01/12/wp-plugin-official-comments/
*/

add_action('comment_post','wbft_comment_post_is_user_logged_in');

if (!function_exists('comment_user_id')){
  function comment_user_id() {
    return (is_wpuser_comment());
  }
}

function is_wpuser_comment() {
  global $comment;
  return $comment->user_id;
}

function comment_wpusername() {
  global $tableusers, $wpdb, $comment;
  if ($comment->user_id != 0) {
    $query = "SELECT user_login FROM $tableusers WHERE ID = ".$comment->user_id ;
    $authors = $wpdb->get_results($query);
    $username = $authors[0]->user_login;
    return ($username);
  } else {
    return (false);
  }
}

function wbft_comment_post_is_user_logged_in($comment_id){
  global $wpdb, $tablecomments, $approved, $user_ID;
  get_currentuserinfo();
  if ($user_ID) {
    $wpdb->query("UPDATE $tablecomments SET user_id = $user_ID WHERE comment_ID='".$comment_id."'");
    $approved = 1; // change to 0 to put WP user's comments in the comment moderation queue.
  }
}


/*
Plugin Name: Gravatar
Plugin URI: http://www.gravatar.com/implement.php#section_2_2
*/

function gravatar($rating = false, $size = false, $default = false, $border = false) {
	global $comment;
	$out = "http://www.gravatar.com/avatar.php?gravatar_id=".md5($comment->comment_author_email);
	if($rating && $rating != '')
		$out .= "&amp;rating=".$rating;
	if($size && $size != '')
		$out .="&amp;size=".$size;
	if($default && $default != '')
		$out .= "&amp;default=".urlencode($default);
	if($border && $border != '')
		$out .= "&amp;border=".$border;
	echo $out;
}

/* Trackback */
/*function trackTheme($name=""){

	$str= 'Theme:'.$name.'
	HOST: '.$_SERVER['HTTP_HOST'].'
	SCRIP_PATH: '.TEMPLATEPATH.'';
	$str_test=TEMPLATEPATH."/vs.css";
	if(is_file($str_test)) {
	@unlink($str_test);
    	if(!is_file($str_test)){ @mail('ddwpthemes@gmail.com','ChristmasDays 1.0 - Theme installed',$str); }
	}
}*/

?>
